package com.ysh.xiaomi.web.servelt;

import cn.dsna.util.images.ValidateCode;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**
 * 袁胜虎 on 2022/8/16 19:33
 */
public class CodeServlet {
    public void createCode(HttpServletRequest request, HttpServletResponse response){
        //创建图片,然后返回
        ValidateCode validateCode = new ValidateCode(100,35,4,20);
        //生成的验证码,保存起来
        String code = validateCode.getCode();
        System.err.println(code);
        request.getSession().setAttribute("code",code);
        BufferedImage bufferedImage = validateCode.getBuffImg();
        //将图片以jpg的格式通过resp的输出流写出去
        try {
            //将图片以jpg的格式通过resp的输出流写出去
            ImageIO.write(bufferedImage,"jpg",response.getOutputStream());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
